<%--
  Created by IntelliJ IDEA.
  User: pjh19
  Date: 12/8/2021
  Time: 上午11:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%
    String path = request.getContextPath();
%>
<html>
<head>
    <script src="${pageContext.request.contextPath}/static/js/jquery-3.1.1.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/bootstrap4.6.0/js/bootstrap.js"></script>
    <link href="<%=path%>/static/bootstrap4.6.0/css/bootstrap.css" rel="stylesheet">
    <style type="text/css">
        img{width:200px;height:200px;}
        #avg2{margin-left:150px}

    </style>
    <title>Title</title>
</head>
<body>
<div class="container">
    <div style="height: 50px">
        <a href="${pageContext.request.contextPath}/goods/flower" class="btn btn-success">花</a>
        <a href="${pageContext.request.contextPath}/goods/food" class="btn btn-success">食物</a>
        <input class="input" id="in"><button class="btn btn-success" id="bt" onclick="goto()">search</button>
        <button class="btn btn-success" onclick="gotoless()">小于</button>
        <button class="btn btn-success" onclick="gotomore()">大于</button>
        <shiro:hasRole name="管理员">
            <button class="btn btn-danger" onclick="gotoshopZhl()">商品管理</button>
            <button class="btn btn-danger" onclick="gotoshopjhs()">订单管理</button>
            <button class="btn btn-danger" onclick="gotoshopjhsuser()">用户管理</button>
        </shiro:hasRole>
        <shiro:authenticated>
            <button class="btn btn-danger" onclick="goout()">登出</button>
        </shiro:authenticated>
        <a href="${pageContext.request.contextPath}/user/shop/cart?username=${sessionScope.loginUser.userName}">
        <svg id="avg2" class="bi bi-cart4" width="2em" height="2em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
            <path fill-rule="evenodd" d="M0 2.5A.5.5 0 0 1 .5 2H2a.5.5 0 0 1 .485.379L2.89 4H14.5a.5.5 0 0 1 .485.621l-1.5 6A.5.5 0 0 1 13 11H4a.5.5 0 0 1-.485-.379L1.61 3H.5a.5.5 0 0 1-.5-.5zM3.14 5l.5 2H5V5H3.14zM6 5v2h2V5H6zm3 0v2h2V5H9zm3 0v2h1.36l.5-2H12zm1.11 3H12v2h.61l.5-2zM11 8H9v2h2V8zM8 8H6v2h2V8zM5 8H3.89l.5 2H5V8zm0 5a1 1 0 1 0 0 2 1 1 0 0 0 0-2zm-2 1a2 2 0 1 1 4 0 2 2 0 0 1-4 0zm9-1a1 1 0 1 0 0 2 1 1 0 0 0 0-2zm-2 1a2 2 0 1 1 4 0 2 2 0 0 1-4 0z"/>
        </svg>
        </a>


    </div>
    <div class="container">
        <table class="table" id="tab">
            <thead>
            <tr>
                <th>商品</th>
                <th>图片</th>
                <th>价格</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${goods.list}" var="one">
                <tr>
                    <th>${one.goodsName}</th>
                    <th><img src="${pageContext.request.contextPath}${one.pImage}.png"></th>
                    <th>${one.price}</th>
                    <th><a href="${pageContext.request.contextPath}/goods/goodsdetail?id=${one.goodsId}">详情</a>
                        <button class="btn btn-warning" onclick="gotoshpping(${sessionScope.loginUser.userId},${one.goodsId},${one.price},'${sessionScope.loginUser.userName}')">购买</button>
                        <button class="btn btn-danger" onclick="gotocart(${sessionScope.loginUser.userId},${one.goodsId})">
                            <svg class="bi bi-cart-plus" width="2em" height="2em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd" d="M8.5 5a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1H8V5.5a.5.5 0 0 1 .5-.5z"/>
                            <path fill-rule="evenodd" d="M8 7.5a.5.5 0 0 1 .5-.5h2a.5.5 0 0 1 0 1H9v1.5a.5.5 0 0 1-1 0v-2z"/>
                            <path fill-rule="evenodd" d="M0 1.5A.5.5 0 0 1 .5 1H2a.5.5 0 0 1 .485.379L2.89 3H14.5a.5.5 0 0 1 .491.592l-1.5 8A.5.5 0 0 1 13 12H4a.5.5 0 0 1-.491-.408L2.01 3.607 1.61 2H.5a.5.5 0 0 1-.5-.5zM3.102 4l1.313 7h8.17l1.313-7H3.102zM5 12a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm7 0a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm-7 1a1 1 0 1 0 0 2 1 1 0 0 0 0-2zm7 0a1 1 0 1 0 0 2 1 1 0 0 0 0-2z"/>
                        </svg>
                        </button>
                                                <shiro:hasRole name="管理员">
                                                    <button onclick="modelshow(${one.goodsId})">添加图片</button>
                                                </shiro:hasRole>
                    </th>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </div>
    <div>
        <button onclick="firstPage()">首页</button>
        <button onclick="previoustPage()">上一页</button>
        <button onclick="nextPage()">下一页</button>
        <button onclick="lastPage()">尾页</button>
        <span>当前第${goods.currentPage}页,共${goods.totalPage}页,共${goods.totalSize}条数据</span>
    </div>
    <script>
        var pageNumber = ${goods.currentPage};
        var pageSize = ${goods.pageSize};
        var totalPage = ${goods.totalPage};
        function gotopage() {


            window.location.href=
                "${pageContext.request.contextPath}/goods/${url}?pageNumber="+pageNumber+"&pageSize="+pageSize+"${url2}"
        }
        function firstPage() {
            if (pageNumber > 1){
                pageNumber = 1;
                gotopage();
            }
        }
        function previoustPage() {
            if (pageNumber > 1){
                pageNumber --;
                gotopage();
            }
        }
        function nextPage() {
            if (pageNumber < totalPage){
                pageNumber ++;
                gotopage();
            }
        }
        function lastPage() {
            if (pageNumber < totalPage){
                pageNumber = totalPage;
                gotopage();
            }
        }
        function gotoshopZhl(){
            window.location.href=
                "${pageContext.request.contextPath}/shopZhl/goods"
        }
        function gotoshopjhs(){
            window.location.href=
                "${pageContext.request.contextPath}/manager/getAll"
        }
        function gotoshopjhsuser(){
            window.location.href=
                "${pageContext.request.contextPath}/userMessage/getUser"
        }
        function goout() {
            window.location.href=
                "${pageContext.request.contextPath}/user/logout"
        }
        function modelshow(id) {
            $("#orderDetails").modal("show");
            $("#modelgoodsId").val(id);

        }
        function gotoshpping(userid,goodsId,price,userName) {
            var userId =userid;
            var goodsId =goodsId;
            var price =price;
            var name = userName;
            console.log(userId,goodsId,price,name)
            window.location.href=
                "${pageContext.request.contextPath}/user/shop/createOrderByGoods?userId="+userId+"&total="+price+"&goodsId="+goodsId+"&name="+name
        }
        function gotocart(userId,goodsId) {
            var userId =userId;
            var goodsId =goodsId;
            console.log(userId,goodsId)
            $.ajax({
                url:"${pageContext.request.contextPath}/user/shop/addCart",
                type:"post",
                contentType:"application/x-www-form-urlencoded",
                data:{userId:userId,goodsId:goodsId},
                dataType: "json",
                success:function (result) {
                    alert(JSON.stringify(result))
                },
                error:function (err) {
                    alert(err)
                }
            })
        }




    </script>
    <div id="orderDetails" class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalCenterTitle">图片上传</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">

                    <div class="form-group">
                        <div class="text-sm-left">
                            <input type="hidden" id="orderId" name="orderId" >
                            <input type="hidden" id="userId" name="userId" >
                            <input type="text" id="modelgoodsId" readonly><br>
                            <input class="input-group-append" type="file" id="someFile">
                            <button onclick="ajaxFile()" class="btn btn-success">上传图片</button><br>
                            <input class="input-group-append" type="text" id="Fileadd" value="/static/image">
                            <button onclick="ajaxFileadd()" class="btn btn-success">图片地址</button>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                </div>
            </div>
        </div>
    </div>

</div>


<script>
    function goto() {
        var sub =$("#in").val();
        window.location.href = "${pageContext.request.contextPath}/goods/byname?search="+sub+""
    }
    function gotoless() {
        var sub =$("#in").val();
        window.location.href = "${pageContext.request.contextPath}/goods/lessBymoney?money="+sub+""
    }
    function gotomore() {
        var sub =$("#in").val();
        window.location.href = "${pageContext.request.contextPath}/goods/moreBymoney?money="+sub+""
    }
    function gotoPage() {
        var sub =$("#in").val();
        window.location.href = "${pageContext.request.contextPath}/goods/getByNamePage?money="+sub+""
    }
    function ajaxsu() {
        var sub =$("#in").val();
        console.log(sub);
        $.ajax({
            url:"byname",
            type:"post",
            contentType:"application/x-www-form-urlencoded",
            data:{search:sub},
            dataType:"json",
            success:function (data) {
                console.log(data)
            },
            error:function (data) {
                console.log("错误")
            }
        })


    }function ajaxFile() {
        //获取文件信息
        var someFile = $("#someFile")[0].files[0];
        var formData =new FormData();
        //第一个参数指定参数名,第二个参数才是上传的文件数据
        formData.append("someFile",someFile);

        //发射ajax请求
        $.ajax({
            url:"${pageContext.request.contextPath}/goods/file",
            type:"post",//数据量大的场景.需要使用post
            processData: false, //不让jquery内部自动处理参数
            contentType:false, //让jQuery内部自行处理 multipart/form-data
            data:formData,
            dataType:"json",
            success:function (result) {
                alert(JSON.stringify(result));

            },
            error:function (err) {
                alert(JSON.stringify(err));
            }
        })

    }
    function ajaxFileadd() {
        var add =$("#Fileadd").val();
        var id = $("#modelgoodsId").val();
        console.log(add,id);
        $.ajax({
            url:"${pageContext.request.contextPath}/goods/photoadd",
            type:"post",
            contentType:"application/x-www-form-urlencoded",
            data:{add:add,id:id},
            dataType: "json",
            success:function (result) {
                alert(JSON.stringify(result));
                window.location.reload();
            },
            error:function (err) {
                alert(err)
            }

        })
    }

</script>

</body>
</html>
